package com.warm.base.jdk.enums;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2019/12/23 14:40
 * @Author: LChuang
 * @Description:
 */
public class EnumTest {

    public static void main(String[] args) throws ClassNotFoundException {
        Class<Enum> c = (Class<Enum>) Class.forName("com.warm.framework.enums.ExcelEnum");
        System.out.println(EnumUtil(c));
    }

    private static List<Map<Integer, String>> EnumUtil(Class<Enum> em){
        List<Map<Integer, String>> list = new ArrayList<>();

        try {
            Method toName = em.getMethod("getDesc");
            Method toCode = em.getMethod("getCode");
            //得到enum的所有实例
            Object[] objs = em.getEnumConstants();

            for(Object obj : objs){
                if("".equals((String)toName.invoke(obj))){
                    Integer code = (Integer)toCode.invoke(obj);
                }
            }

            for (Object obj : objs) {
                Map<Integer, String> map = new HashMap<>();
                map.put((Integer)toCode.invoke(obj), (String)toName.invoke(obj));
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
